package oralExamSchedulingSystem;

import java.util.*;

import oralExamSchedulingSystem.Time;
import java.util.Date;

public class Timetable {
	final private String _subject;
	private String _reportingRoom;
	private String _Date;
	private int _startTime;
	private int _duration;
	private int _groupSize;
	private int _numberOfGroup;
	private int _groupsPerTimeslot;
	private LinkedList<String> _room;
	private ArrayList<ArrayList<Group>> _Schedule;

	public Timetable(String subject,String reportingRoom,String date,int startTime,int duration,int groupSize,int numberOfGroup,int groupsPerTimeslot,LinkedList<String> _room) {
		this._subject = subject;
		this._reportingRoom = reportingRoom;
		this._Date = date;
		this._startTime = startTime;
		this._duration = duration;
		this._groupSize = groupSize;
		this._numberOfGroup = numberOfGroup;
		this._groupsPerTimeslot = groupsPerTimeslot;
		
		_Schedule = new ArrayList<ArrayList<Group>>(); // For one timeslot only
		
		int groupCount=0; //for counting the number of group to be added
		int timeslotCount=0; //for counting the number of timeslot
		while(groupCount<_numberOfGroup){
			ArrayList<Group> timeslot=new ArrayList<Group>(); //create a new timeslot
			int groupCountInATimeslot=0; //reset the number of group in a timeslot
			Iterator<String> roomNum_iter=_room.iterator(); //get the iterator of the roomNum string
			
			while(groupCountInATimeslot<_groupsPerTimeslot && roomNum_iter.hasNext() && groupCount<_numberOfGroup){
				String roomNum = roomNum_iter.next(); //get the string of a room
				timeslot.add(new Group(roomNum,groupCount,getTimeslotTimeOject(timeslotCount,0),this._subject,_groupSize)); //add group into a timeslot
				groupCountInATimeslot++; //increment whenever a group is added to a timeslot
				groupCount++; //increment whenever a group is added
			}
			timeslotCount++;//increment after adding groups into a timeslot
			_Schedule.add(timeslot); //put the timeslot into the timetable
		}
	}

	/**
	 * Gets the timeslot time oject.
	 *
	 * @param timeslot : the first timeslot is 0
	 * @param offset the offset
	 * @return the timeslot time oject
	 */
	private Time getTimeslotTimeOject(int timeslot,int offset){
		return new Time(_startTime+_duration*timeslot+offset,_duration+_duration*timeslot);
	}
	
	public String getReportingRoom() {
		return this._reportingRoom;
	}

	public String getDate() {
		return this._Date;
	}

	public ArrayList<ArrayList<Group>> getSchedule() {
		return _Schedule;
		
//		ArrayList<Group> timeslot = _Schedule.get(0);
//		Iterator<Group> iter = timeslot.iterator();
//
//		while (iter.hasNext()) {
//			Group g = iter.next();
//		}
//		return _Schedule;
	}

	public void setReportingRoom(String _reportingRoom) {
		this._reportingRoom = _reportingRoom;
	}

	public void setDate(String _Date) {
		this._Date = _Date;
	}

	public void setSchedule(int _examTime, int _roomNumber, Group group) {
		// this._Schedule[_examTime][_roomNumber] = group;
	}
	
	public Group getGroupByID(int id){
		Iterator<ArrayList<Group>> timeslot_iter = _Schedule.iterator();
		while(timeslot_iter.hasNext()){
			ArrayList<Group> timeslot=timeslot_iter.next();
			Iterator<Group> group_iter=timeslot.iterator();
			while(group_iter.hasNext()){
				Group grp=group_iter.next();
				if(id==grp.getGroupID())
					return grp;
			}
		}
		return null;
	}
	
	public int getNumberOfGroup(){
		return _numberOfGroup;
	}
	
//	public boolean appendGroupIntoTimeTable(ArrayList<Student> studentList){
//		if (studentList.size()>_groupSize)
//			return false;
//		ArrayList<Group> lastTimeSlot=_Schedule.get(_Schedule.size()-1); //get the last Time slot
//		if(lastTimeSlot.size()==_groupsPerTimeslot){ //if the last time slot is full, create new time slot
//			
//		}else{ //if the last time slot is not full, just insert into the time slot
//		}
//		return true;
//	}
}
